package C10_9_IntroduceParameterObject.after_refactor;

import C10_9_IntroduceParameterObject.Entry;

import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;

/**
 * 账目
 */
public class Account {
    private Vector _entries = new Vector();

    /**
     * 重构1
     * @param range
     * @return
     */
    double getFlowBetween(DateRange range) {
        double result = 0;
        Enumeration e = _entries.elements();
        while (e.hasMoreElements()) {
            Entry each = (Entry) e.nextElement();
            if (each.getDate().equals(range.getStart()) || each.getDate().equals(range.getEnd()) ||
                    (each.getDate().after(range.getStart()) && each.getDate().before(range.getEnd()))) {
                result += each.getValue();
            }
        }
        return result;
    }

    /**
     * 重构2
     * @param range
     * @return
     */
    double getFlowBetween2(DateRange range) {
        double result = 0;
        Enumeration e = _entries.elements();
        while (e.hasMoreElements()) {
            Entry each = (Entry) e.nextElement();
            if (range.includes(each.getDate())) {
                result += each.getValue();
            }
        }
        return result;
    }

}
